package org.example.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.example.service.IAlipayService;
import org.example.service.IOrderService;
import org.example.util.AliPayUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
szy

*/
@RestController
@RequestMapping("/ali")
public class AlipayController {
    @Resource
    private IAlipayService iAlipayService;

    @Resource
    private AliPayUtil aliPayUtil;

    @Resource
    private IOrderService iOrderService;

    @RequestMapping("/notifyPay")
    public void alipay(HttpServletRequest request) throws AlipayApiException {
        Map<String, String> stringStringMap = aliPayUtil.convertRequestParamsToMap(request);
        boolean  signVerified = AlipaySignature.rsaCheckV1(stringStringMap, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjbilapEpHt+dyn7RjATaTO9ViuXyMP5SoHPnbG7PtPppZFhu6McOvjzGrhJTGbUu/hvocou8I1cc+ptmEK44cb6BymjE8j/0oVNt91nX8VHtxP/zrRxBFr9dw+fNr1keEJOfNIz/dcTkZtYGng2TgzV0BB/vbho50ysUXRPpqvp6YOkJyEFN9uKquH8cWhJrG6horyxTc3rl6CNHsff5yCN29BuIcG1iY3O8/+GQ/lYfkEASJeL74CUbbN7vI0tiR+zBSViGg3J88Lo+06CSvheV8PtRFZCV0Md5q6E2bez5MykJrzxvfUjWaW/CKamCvfFIK4JDIEuPG89ZI3PX/QIDAQAB", "utf-8", "RSA2");  //调用SDK验证签名
        if (signVerified){
            System.out.println(stringStringMap);
            if (stringStringMap.get("total_amount").equals(stringStringMap.get("receipt_amount"))){
                iAlipayService.alipay(stringStringMap);
                String s = stringStringMap.get("out_trade_no");
                System.out.println(s);

            }
        }
        else {
            System.out.println("失败");
        }
    }

}
